/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/util/MapUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: MapUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author elancom
 * @date Apr 16, 2012
 */
public abstract class MapUtils {
	public static <K, T> Map<K, T> buildHashMap(K[] keys, T[] values) {
		if (keys == null || values == null || keys.length != values.length) {
			return null;
		}

		final Map<K, T> hash = new HashMap<K, T>();
		for (int i = 0; i < keys.length; i++) {
			hash.put(keys[i], values[i]);
		}

		return hash;
	}

	public static <K, V, T> Map<K, T> copy(Map<K, V> source, Map<K, T> target) {
		if (source == null) {
			return null;
		}

		if (target == null) {
			target = new HashMap<K, T>();
		}

		final Map<K, T> _target = target;
		JoyfulUtils.iterEach(source, new IterEach<K, V>() {
			@SuppressWarnings("unchecked")
			@Override
			public boolean each(K key, V value) {
				_target.put(key, (T) value);
				return true;
			}
		});

		return _target;
	}

	public static <K, V> V getValueIfNull(V defValue, Map<K, V> map, K key) {
		return getValueIfTrue(map, key, defValue, defValue == null);
	}

	public static <K, V> V getValueIfTrue(Map<K, V> map, K key, V defValue, boolean doGet) {
		if (doGet && map != null) {
			return map.get(key);
		}

		return defValue;
	}

	public static <K, V> Map<K, V> createMap(Class<K> key, Class<V> value) {
		return new HashMap<K, V>();
	}

	public static <K, V> Map<K, V> createMap(K key, V value) {
		return createMap(key, value, null, null);
	}

	public static <K, V> Map<K, V> createMap(K key1, V value1, K key2, V value2) {
		return createMap(key1, value1, key2, value2, null, null);
	}

	public static <K, V> Map<K, V> createMap(K key1, V value1, K key2, V value2, K key3, V value3) {
		final Map<K, V> map = new HashMap<K, V>();

		if (key1 != null) {
			map.put(key1, value1);
		}

		if (key2 != null) {
			map.put(key2, value2);

		}

		if (key3 != null) {
			map.put(key3, value3);
		}
		return map;
	}

	public static Map<String, Object> createMap(Object... obj) {
		final Map<String, Object> map = new HashMap<String, Object>();
		if (obj == null || obj.length == 0) {
			return map;
		}

		final int len = obj.length % 2 == 0 ? obj.length : obj.length - 1;
		for (int i = 0; i < len; i++) {
			if (obj[i] != null) {
				map.put(obj[i].toString(), obj[++i]);
			}
		}

		return map;
	}

	public static <K, V> void debug(Map<K, V> map) {
		if (map == null) {
			return;
		}

		final StringBuilder sb = new StringBuilder("\n");
		sb.append("map debug:\n");
		sb.append("--------------------------------------------------------\n");
		JoyfulUtils.iterEach(map, new IterEach<K, V>() {

			private void append(Object obj, StringBuilder sb) {
				if (obj instanceof Object[]) {
					for (final Object o : (Object[]) obj) {
						append(o, sb);
						sb.append(",");
					}
				} else {
					sb.append(obj.toString());
				}
			}

			@Override
			public boolean each(K key, V value) {
				sb.append("\t").append(key).append("\t:");
				append(value, sb);
				sb.append("\n");
				return true;
			}
		});

		sb.append("--------------------------------------------------------\n");
	}

	public static <K, T> Integer getInteger(Map<K, T> map, K key) {
		final String str = getString(map, key);
		return Integer.valueOf(str);
	}

	public static <K, T> String getString(Map<K, T> map, K key) {
		final T object = getValue(map, key);
		return object == null ? null : object.toString();
	}

	public static <K, T> T getValue(Map<K, T> map, K key) {
		if (map != null) {
			return map.get(key);
		}
		return null;
	}

	public static boolean isEmpty(Map<?, ?> map) {
		return map == null || map.isEmpty();
	}
}
